/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2013-11-27         luwei
 */

package com.coocaa.app.gamecenter.pages.home;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.widget.ScrollView;

import com.coocaa.app.gamecenter.framework.ui.GameCenterStatusBar;
import com.coocaa.launcher.framework.sidebar.ItemContainer.IItem;
import com.coocaa.launcher.sidebar.setting.layout.wallpaper.OnKeyTurnPageListener;
import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.widget.SkyAppPager;
import com.tianci.appstore.ui.widget.SkyAppPager.AnimListener;
import com.tianci.appstore.ui.widget.SkyBaseItemView;
import com.tianci.appstore.ui.widget.SkyCustomLayout;

import java.util.List;


public class GameCenterController implements OnKeyTurnPageListener, AnimListener {
    private SkyAppPager mPager = null;
    private int mTotalPage = 0;
    private int mCurrentPage = 0;
    private Context mContext = null;
    private int mCurrentPos = 0;
    private GameCenterStatusBar mStatusBar = null;
    private GameCenterLayout mGameCenterLayout = null;
    private int mKeyCode = KeyEvent.KEYCODE_DPAD_DOWN;

    public GameCenterController(final Context context) {
        this.mContext = context;
        mGameCenterLayout = new GameCenterLayout(mContext);
        this.mPager = mGameCenterLayout.getAppPager();
        this.mStatusBar = mGameCenterLayout.getStatusBar();
        mPager.setAnimListener(this);
        mStatusBar.initProgressbar(2.0f);
        ((RecommendAdapter) getPageByIndex(0).getAdapter()).setKeyTurnPageListener(this);
        setMyGameFocus();
        hideFocusView();
    }

    public GameCenterLayout getLayout() {
        return mGameCenterLayout;
    }

    public void hideFocusView() {
        mGameCenterLayout.hideFocusView();
    }

    public void showFocusView() {
        mGameCenterLayout.showFocusView();
    }

    public void setMyGameFocus() {
        mGameCenterLayout.setMyGameFocus();
    }

    public void refreshRecommand(List<SkyListAppData> baseData) {
        mTotalPage++;
        LogUtils.i("luwei", "the recommand game is " + baseData.size());
        ((RecommendAdapter) getPageByIndex(0).getAdapter()).updateUI(baseData);
        if (mGameCenterLayout.getParent() != null) {
            initFocus();
        }
    }

    public void addCategoryLayout(final CategoryData categoryData) {
        mTotalPage++;
        mGameCenterLayout.addCategoryLayout(categoryData);
        ((CategoryAdapter) getPageByIndex(1).getAdapter()).setKeyTurnPageListener(GameCenterController.this);
        ((CategoryAdapter) getPageByIndex(1).getAdapter()).updateUI(categoryData.mCategoryDatas);
    }

    public void initFocus() {
        mGameCenterLayout.initFocus();
    }

    private void showNextPage() {
        mCurrentPage++;
        mGameCenterLayout.setBtnFocus(false);
        mPager.displayNextPage();
        mPager.showNext();
        mStatusBar.moveNext();
    }


    private void showPrePage() {
        mCurrentPage--;
        mGameCenterLayout.setBtnFocus(false);
        mPager.displayPrePage();
        mPager.showPrevious();
        mStatusBar.movePre();
    }

    public static final int SET_FOCUS_POS = 0;
    public static final int REFRESH_RECOMMEND = 1;
    public static final int REFRESH_CATEGORY = 2;
    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SET_FOCUS_POS:
                    getCurrentPage().setFocusPos(msg.arg1, true);
                    break;
                case REFRESH_CATEGORY:
                    CategoryData categoryData = (CategoryData) msg.obj;
                    mTotalPage++;
                    mGameCenterLayout.addCategoryLayout(categoryData);
                    ((CategoryAdapter) getPageByIndex(1).getAdapter()).setKeyTurnPageListener(GameCenterController.this);
                    ((CategoryAdapter) getPageByIndex(1).getAdapter()).updateUI(categoryData.mCategoryDatas);
                    break;
                case REFRESH_RECOMMEND:
                    mTotalPage++;
                    List<SkyListAppData> baseData = (List<SkyListAppData>) msg.obj;
                    LogUtils.i("luwei", "the recommand game is " + baseData.size());
                    ((RecommendAdapter) getPageByIndex(0).getAdapter()).updateUI(baseData);
                    break;
                default:
                    break;
            }
        }

        ;
    };

    public boolean onItemKeyUp(int index, boolean turnPage) {
        LogUtils.d("Controller, OnKeyLeft and the index is " + index);
        if (turnPage && mCurrentPage > 0) {
            showPrePage();
            mGameCenterLayout.showDownArrow();
            return true;
        }
        return false;
    }


    private SkyCustomLayout getCurrentPage() {
        ViewGroup view = (ViewGroup) mPager.getChildAt(mPager.getDisplayedChild());
        if (view instanceof ScrollView) {
            return (SkyCustomLayout) view.getChildAt(0);
        }
        return (SkyCustomLayout) view;
    }

    private SkyCustomLayout getPageByIndex(int index) {
        ViewGroup view = (ViewGroup) mPager.getChildAt(index);
        if (view instanceof ScrollView) {
            return (SkyCustomLayout) view.getChildAt(0);
        }
        return (SkyCustomLayout) view;
    }

    public boolean onItemKeyDown(int index, boolean turnPage) {
        LogUtils.d("Controller, OnKeyRight and the index is " + index);
        if (turnPage && mCurrentPage < mTotalPage - 1) {
            showNextPage();
            mGameCenterLayout.hideDownArrow();
            return true;
        }
        return false;
    }

    public boolean onItemKeyLeft(int index, boolean turnPage) {
        LogUtils.d("Controller, OnKeyLeft and the index is " + index);
        if (turnPage && mCurrentPage > 0) {
            showPrePage();
            return true;
        }
        return false;
    }

    public boolean onItemKeyRight(int index, boolean turnPage) {
        LogUtils.d("Controller, OnKeyRight and the index is " + index);
        if ((turnPage || index == getCurrentPage().getChildCount() - 1) && mCurrentPage < mTotalPage - 1) {
            showNextPage();
            return true;
        }
        return false;
    }

    public void updateUI() {

    }
//
//    @Override
//    public void onClick(int index)
//    {
//        
//    }

    @Override
    public boolean onAnimStart(int current) {
        SkyBaseItemView.mKey = false;
        return false;
    }

    @Override
    public boolean onAnimEnd(int current) {
        SkyBaseItemView.mKey = true;
        int pos = 0;
        if (mKeyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
            if (mCurrentPos == 2) {
                pos = 0;
            } else if (mCurrentPos == 3) {
                pos = 1;
            } else if (mCurrentPos == 4) {
                pos = 3;
            }
            getPageByIndex(1).setFocusPos(pos, true);
        } else if (mKeyCode == KeyEvent.KEYCODE_DPAD_UP) {
            if (mCurrentPos == 0 || mCurrentPos == 1) {
                pos = 0;
            } else if (mCurrentPos == 2 || mCurrentPos == 3) {
                pos = 1;
            }
            getPageByIndex(0).setFocusPos(pos, true);
        }
        mGameCenterLayout.setBtnFocus(true);
        //mHandler.sendMessageDelayed(msg, 100);
        return false;
    }

    @Override
    public boolean onKeyDown(IItem item, int keyCode, int index, boolean[] turnPage) {
        mCurrentPos = item.getIndex();
        mKeyCode = keyCode;
        switch (keyCode) {
//            case KeyEvent.KEYCODE_DPAD_LEFT:
//                return onItemKeyLeft(index, turnPage[0]);
//            case KeyEvent.KEYCODE_DPAD_RIGHT:
//                return onItemKeyRight(index, turnPage[2]);
            case KeyEvent.KEYCODE_DPAD_UP:
                return onItemKeyUp(index, turnPage[1]);
            case KeyEvent.KEYCODE_DPAD_DOWN:
                return onItemKeyDown(index, turnPage[3]);
            default:
                break;
        }
        return false;
    }

    public void destory() {
        try {
            for (int i = 0; i < mTotalPage; i++) {
                getPageByIndex(i).getAdapter().destory();
            }
            mGameCenterLayout.destory();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
